package com.wxg.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.wxg.tools.cmd.JavaRuntimeExec;

public class WinProcessUtils {

	/**
	 * 这个是获取windows下进程ID的方法。
	 * 利用了`netstat -aon | findstr 8080`命令。
	 * 然后对结果进行解析。 最终得到PID。 
	 * 
	 * 
C:\Users\wxg>netstat -aon | findstr "5000"
  TCP    0.0.0.0:5000           0.0.0.0:0              LISTENING       14480
  TCP    127.0.0.1:5000         127.0.0.1:56546        ESTABLISHED     14480
  TCP    127.0.0.1:5000         127.0.0.1:56547        ESTABLISHED     14480
  TCP    127.0.0.1:5000         127.0.0.1:56548        ESTABLISHED     14480
  TCP    127.0.0.1:5000         127.0.0.1:56549        ESTABLISHED     14480
  TCP    127.0.0.1:5000         127.0.0.1:56550        ESTABLISHED     14480
  TCP    127.0.0.1:5000         127.0.0.1:56551        ESTABLISHED     14480
  TCP    127.0.0.1:50000        0.0.0.0:0              LISTENING       20792
  TCP    127.0.0.1:56502        127.0.0.1:5000         TIME_WAIT       0
  TCP    127.0.0.1:56546        127.0.0.1:5000         ESTABLISHED     10508
  TCP    127.0.0.1:56547        127.0.0.1:5000         ESTABLISHED     10508
  TCP    127.0.0.1:56548        127.0.0.1:5000         ESTABLISHED     10508
  TCP    127.0.0.1:56549        127.0.0.1:5000         ESTABLISHED     10508
  TCP    127.0.0.1:56550        127.0.0.1:5000         ESTABLISHED     10508
  TCP    127.0.0.1:56551        127.0.0.1:5000         ESTABLISHED     10508
	 * 
	 * 
C:\Users\wxg>netstat -aon | findstr "5000"
  TCP    0.0.0.0:5000           0.0.0.0:0              LISTENING       14480
  TCP    127.0.0.1:50000        0.0.0.0:0              LISTENING       20792
  TCP    127.0.0.1:56546        127.0.0.1:5000         TIME_WAIT       0
  TCP    127.0.0.1:56547        127.0.0.1:5000         TIME_WAIT       0
  TCP    127.0.0.1:56548        127.0.0.1:5000         TIME_WAIT       0
  TCP    127.0.0.1:56549        127.0.0.1:5000         TIME_WAIT       0
  TCP    127.0.0.1:56550        127.0.0.1:5000         TIME_WAIT       0
  TCP    127.0.0.1:56551        127.0.0.1:5000         TIME_WAIT       0
	 * 
	 * 
	 * @param port
	 * @return
	 */
	public static String getWinPid(int port){
		String pid = "";
		JavaRuntimeExec jre = new JavaRuntimeExec();
		String commandStr = "cmd /c netstat -aon | findstr " + String.valueOf(port);
		String[] commands = commandStr.split(" ");
		String dir = System.getProperty("user.dir");
		jre.runtimeExec(commands, dir);
		BufferedReader br = jre.fetchReader();
		String line = null;
		List<String> list = new ArrayList<String>();
		try {
			while((line=br.readLine())!=null){
				list.add(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		jre.destroy();
		
		if(list.size() > 0){
			String regex = ".*:(" + port + "){1,}\\s+.*";
			String regex2 = ".*\\s+LISTENING\\s+.*";
			String[] strArr = {};
			for(String str : list){
				if(str.matches(regex) && str.matches(regex2)){
					strArr = str.split("\\s+");
					pid = strArr[strArr.length-1];
					break;
				}
			}
		}
		
		return pid.trim();
	}
	
	/**
	 * 
	 * 根据PID杀死进程！
	 * 利用`TASKKILL /F /PID 14616 /T`
	 * 
	 * @param pid
	 */
	public static void killWinProcess(String pid){
		JavaRuntimeExec jre = new JavaRuntimeExec();
		String commandStr = "TASKKILL /F /PID " + pid + " /T";
		String[] commands = commandStr.split(" ");
		String dir = System.getProperty("user.dir");
		jre.runtimeExec(commands, dir);
		java.io.BufferedReader br = jre.fetchReader();
		String line = null;
		try {
			System.out.printf("pid: %s\n", pid);
			while((line=br.readLine())!=null){
				//System.out.println(new String(line.getBytes("gbk")));
				System.out.println(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		jre.destroy();
	}
	
	public static Process startProcess(String command, String dir){
		JavaRuntimeExec jre = new JavaRuntimeExec();
		String[] commands = command.split(" ");
		return jre.runtimeExec(commands, dir);
	}
	
	public static void stopProcess(String port){
		killWinProcess(getWinPid(Integer.valueOf(port)));
	}
	
}
